package com.inno.sms.controller;

import com.inno.sms.annotiation.Hidden;
import com.inno.sms.dto.SmsInfoDTO;
import com.inno.sms.response.CommonReturnType;
import com.inno.sms.service.PlannerSmsInfoService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author sean
 */
@RestController
@RequestMapping("/sms")
@RequiredArgsConstructor
public class SmsController {
    private final PlannerSmsInfoService plannerSmsInfoService;

    @PostMapping("/send")
    @Hidden
    public CommonReturnType<Void> sendSms(@RequestBody SmsInfoDTO smsInfoDTO) {
        if (StringUtils.isBlank(smsInfoDTO.getSmsContent())){
            return CommonReturnType.fail("短信内容不能为空");
        }
        if (StringUtils.isBlank(smsInfoDTO.getSmsType())){
            return CommonReturnType.fail("短信类型不能为空");
        }
        /*if (Objects.isNull(smsInfoDTO.getUserId())){
            return CommonReturnType.fail("用户id不能为空");
        }*/
        if (StringUtils.isBlank(smsInfoDTO.getMobile())){
            return CommonReturnType.fail("手机号不能为空");
        }
        try {
            this.plannerSmsInfoService.sendSms(smsInfoDTO);
            return CommonReturnType.success();
        }catch (Exception e){
            return CommonReturnType.fail(e.getMessage());
        }
    }

    @PostMapping("/validate")
    @Hidden
    public CommonReturnType<Boolean> validateSms(@RequestBody SmsInfoDTO smsInfoDTO) {
        if (StringUtils.isBlank(smsInfoDTO.getMobile())){
            return CommonReturnType.fail("手机号不能为空");
        }

        return CommonReturnType.success(this.plannerSmsInfoService.validate(smsInfoDTO));
    }



}
